<?php

/**
 * Article form base class.
 *
 * @method Article getObject() Returns the current form's model object
 *
 * @package    SdCommerce
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormGeneratedTemplate.php 29553 2010-05-20 14:33:00Z Kris.Wallsmith $
 */
abstract class BaseArticleForm extends BaseFormDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                     => new sfWidgetFormInputHidden(),
      'id_categorie'           => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Categorie'), 'add_empty' => false)),
      'id_unite'               => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Unite'), 'add_empty' => false)),
      'id_tva'                 => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Tva'), 'add_empty' => false)),
      'reference'              => new sfWidgetFormInputText(),
      'code_barre'             => new sfWidgetFormInputText(),
      'designation'            => new sfWidgetFormInputText(),
      'description'            => new sfWidgetFormTextarea(),
      'caracteristique'        => new sfWidgetFormTextarea(),
      'prix_uv_ht'             => new sfWidgetFormInputText(),
      'prix_uv_ttc'            => new sfWidgetFormInputText(),
      'marge'                  => new sfWidgetFormInputText(),
      'marge_pourcent'         => new sfWidgetFormInputText(),
      'prix_ua_ht'             => new sfWidgetFormInputText(),
      'prix_ua_ttc'            => new sfWidgetFormInputText(),
      'mont_rem_max'           => new sfWidgetFormInputText(),
      'taux_rem_max'           => new sfWidgetFormInputText(),
      'inactif'                => new sfWidgetFormInputCheckbox(),
      'service'                => new sfWidgetFormInputCheckbox(),
      'cout_revient'           => new sfWidgetFormInputText(),
      'frai_art'               => new sfWidgetFormInputText(),
      'created_at'             => new sfWidgetFormDateTime(),
      'updated_at'             => new sfWidgetFormDateTime(),
      'm_fournisseur_list'     => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Fournisseur')),
      'm_departement_list'     => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Departement')),
      'categories_remise_list' => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'CategorieRemise')),
      'tarifs_list'            => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Tarif')),
      'uniteslogistique_list'  => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'UniteLogistique')),
    ));

    $this->setValidators(array(
      'id'                     => new sfValidatorChoice(array('choices' => array($this->getObject()->get('id')), 'empty_value' => $this->getObject()->get('id'), 'required' => false)),
      'id_categorie'           => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Categorie'))),
      'id_unite'               => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Unite'))),
      'id_tva'                 => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Tva'))),
      'reference'              => new sfValidatorString(array('max_length' => 50)),
      'code_barre'             => new sfValidatorString(array('max_length' => 30, 'required' => false)),
      'designation'            => new sfValidatorString(array('max_length' => 255)),
      'description'            => new sfValidatorString(array('max_length' => 1000, 'required' => false)),
      'caracteristique'        => new sfValidatorString(array('max_length' => 1000, 'required' => false)),
      'prix_uv_ht'             => new sfValidatorNumber(array('required' => false)),
      'prix_uv_ttc'            => new sfValidatorNumber(array('required' => false)),
      'marge'                  => new sfValidatorNumber(array('required' => false)),
      'marge_pourcent'         => new sfValidatorNumber(array('required' => false)),
      'prix_ua_ht'             => new sfValidatorNumber(array('required' => false)),
      'prix_ua_ttc'            => new sfValidatorNumber(array('required' => false)),
      'mont_rem_max'           => new sfValidatorNumber(array('required' => false)),
      'taux_rem_max'           => new sfValidatorNumber(array('required' => false)),
      'inactif'                => new sfValidatorBoolean(array('required' => false)),
      'service'                => new sfValidatorBoolean(array('required' => false)),
      'cout_revient'           => new sfValidatorNumber(array('required' => false)),
      'frai_art'               => new sfValidatorNumber(array('required' => false)),
      'created_at'             => new sfValidatorDateTime(),
      'updated_at'             => new sfValidatorDateTime(),
      'm_fournisseur_list'     => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Fournisseur', 'required' => false)),
      'm_departement_list'     => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Departement', 'required' => false)),
      'categories_remise_list' => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'CategorieRemise', 'required' => false)),
      'tarifs_list'            => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Tarif', 'required' => false)),
      'uniteslogistique_list'  => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'UniteLogistique', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('article[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    $this->setupInheritance();

    parent::setup();
  }

  public function getModelName()
  {
    return 'Article';
  }

  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['m_fournisseur_list']))
    {
      $this->setDefault('m_fournisseur_list', $this->object->MFournisseur->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['m_departement_list']))
    {
      $this->setDefault('m_departement_list', $this->object->MDepartement->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['categories_remise_list']))
    {
      $this->setDefault('categories_remise_list', $this->object->CategoriesRemise->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['tarifs_list']))
    {
      $this->setDefault('tarifs_list', $this->object->tarifs->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['uniteslogistique_list']))
    {
      $this->setDefault('uniteslogistique_list', $this->object->uniteslogistique->getPrimaryKeys());
    }

  }

  protected function doSave($con = null)
  {
    $this->saveMFournisseurList($con);
    $this->saveMDepartementList($con);
    $this->saveCategoriesRemiseList($con);
    $this->savetarifsList($con);
    $this->saveuniteslogistiqueList($con);

    parent::doSave($con);
  }

  public function saveMFournisseurList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['m_fournisseur_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->MFournisseur->getPrimaryKeys();
    $values = $this->getValue('m_fournisseur_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('MFournisseur', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('MFournisseur', array_values($link));
    }
  }

  public function saveMDepartementList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['m_departement_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->MDepartement->getPrimaryKeys();
    $values = $this->getValue('m_departement_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('MDepartement', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('MDepartement', array_values($link));
    }
  }

  public function saveCategoriesRemiseList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['categories_remise_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->CategoriesRemise->getPrimaryKeys();
    $values = $this->getValue('categories_remise_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('CategoriesRemise', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('CategoriesRemise', array_values($link));
    }
  }

  public function savetarifsList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['tarifs_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->tarifs->getPrimaryKeys();
    $values = $this->getValue('tarifs_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('tarifs', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('tarifs', array_values($link));
    }
  }

  public function saveuniteslogistiqueList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['uniteslogistique_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->uniteslogistique->getPrimaryKeys();
    $values = $this->getValue('uniteslogistique_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('uniteslogistique', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('uniteslogistique', array_values($link));
    }
  }

}
